package espaceVisuCarre.java2Dutil;


import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.AffineTransform;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Vector;

import javax.swing.JCheckBox;
import javax.swing.JPanel;

import Interface.FrameDemo;

import xmlItunes.Bibliotheque;
import xmlItunes.Track;
import espaceVisuCarre.Espace;

 
public class JCanvas extends JPanel  implements MouseListener
{
	private static final long serialVersionUID = 1L;

	private List<GraphicListener> listeners ;
	private List<IDrawable> drawables ;
	private AffineTransform baseAT ;

	private FrameDemo framedemo ;
	
	private Espace espace ;
	
	public JCanvas(FrameDemo fenetre)
	{
		super();
		
		this.framedemo = fenetre;
		
		this.addMouseListener(this);
		drawables = new LinkedList<IDrawable>();
		listeners = new LinkedList<GraphicListener>();
		
		baseAT = new AffineTransform();
		
		this.espace = new Espace( this.framedemo.getSaBib() , this );
		
	}

	public void addListener(GraphicListener gl)
	{
		this.listeners.add(gl);
	}
	
    public void addDrawable(IDrawable d) 
    {
    	if( d == null )return;
        drawables.add(d);
        repaint();
    }


    public void removeDrawable(IDrawable d) 
    {
    	if( d == null )return;
        drawables.remove(d);
        repaint();
    }

    public synchronized void paint(Graphics g) 
    {
    		Graphics2D screen2D = (Graphics2D)g ;   	
	    	super.paint(screen2D);   	
	    	
	    	
	    	
	    	screen2D.setTransform(baseAT);
	    	
	        for (IDrawable iter : drawables) 
	        {
	           iter.draw(screen2D);
	        }
	        
	        if(baseAT.getScaleX() == 1 && baseAT.getScaleY() ==1)this.centrer();
    }

    public void clear() 
    {
        drawables.clear();
        repaint();
    }

    public void afficherLegendes(boolean ouinon)
    {
    	this.espace.setAfficherLegende(ouinon);
    }
    
    public void selectionCritereAbcisse( String critAbc )
    {
    	this.espace.setCritereLargeur(critAbc);
    }

    public void selectionCritereOrdonnee( String critOrd )
    {
    	this.espace.setCritereHauteur(critOrd);
    }
    
    public void selectionGenreAnnee(HashMap<String, JCheckBox> listeCheckBoxGenres , int min , int max) 
    {
    	for( GraphicListener gl : this.listeners )
		{
			gl.selectionGenreAnnee(listeCheckBoxGenres, min , max);
		}
    }
    
    public void centrer()
    {
		//baseAT.scale(1/1.1, 1/1.1);
		baseAT = new AffineTransform();
		repaint();
		return ;
    }
    
	@Override
	public void mouseClicked(MouseEvent e) 
	{
		//System.out.println("button = >" + e.getButton());
		if(e.getButton() == 3)
		{
			float cx = this.getSize().width / 2 ;
			float cy = this.getSize().height / 2 ;
			
			//
			
			baseAT.translate(  
								    1.0*(cx-e.getX())   ,
								    1.0*(cy-e.getY())
							);

			baseAT.scale(1.1, 1.1);
			
			repaint();
			return ;
		}
		 if(e.getButton() == 2)
		{
			 centrer();
		}
		 
		 if( e.getButton() == 1 )
		 {

			Point p = e.getPoint();		
		
			LinkedList<IDrawable> clicke = new LinkedList<IDrawable>();
			
			for( IDrawable d : this.drawables )
			{
				if(d.contains(p))
				{					
					clicke.add(d);
				}
			}
			
			IDrawable min = clicke.get(0);
			for( IDrawable d : clicke )
			{
				if( d.getTracks().size() < min.getTracks().size() )
				{
					min = d ;
				}
			}

			Random rand = new Random();
			Track t = (new Vector<Track>(min.getTracks()) ).elementAt( rand.nextInt( min.getTracks().size() ) );	 
			System.out.println(t.getAttribute("Name"));
			
			String pathMP3Courant = t.getAttribute("Location");
			System.out.println(pathMP3Courant);
	    	//on met le path au bon format :
	    	pathMP3Courant = pathMP3Courant.substring(17, pathMP3Courant.length()); 	
	    	pathMP3Courant = pathMP3Courant.replaceAll("/", "\\\\\\\\");
	    	pathMP3Courant = pathMP3Courant.replaceAll("%20", " ");
	    	pathMP3Courant = pathMP3Courant.replaceAll("%C3%A9", "�");
	    	
	    	framedemo.getPanelOutils().getPanelLecteurmp3().selectSong(pathMP3Courant);
		 }
	}


	@Override
	public void mouseEntered(MouseEvent e) 
	{
		Point p = e.getPoint();
		//this.centrer();
	}


	@Override
	public void mouseExited(MouseEvent e) 
	{
		Point p = e.getPoint();
		//this.centrer();
	}


	@Override
	public void mousePressed(MouseEvent e)
	{
		Point p = e.getPoint();
		//System.out.println("mousePressed "+p);
	}


	@Override
	public void mouseReleased(MouseEvent e) 
	{
		Point p = e.getPoint();
		//System.out.println("mouseReleased "+p);
	}


 

}
